<?php

	$width = 225;
	
	include ("reqvars.php");
	
	$t = stripslashes($t);
	$t = substr($t, 0, 255);

	$basedir = "./paths";
	
	$registry = getRegistry($basedir);

	$randomnum = $n;

	include ($registry[$randomnum][1]);

	$map = $registry[$randomnum][0];
	
	list($width_orig, $height_orig) = getimagesize($map);
	
	
	$s = stripslashes($s);
	
	switch($s)
	{
		case 0:
		{
			$width=225;
			break;
		}
		case 1:
		{
			$width=640;
			if ($width>$width_orig)
				$width = $width_orig;
			break;
		}
		case 2:
		{
			$width=$width_orig;
			break;
		}
	}
	
	
	$ratio_orig = $width_orig/$height_orig;

	$height = $width/$ratio_orig;
	
	$image_p = imagecreatetruecolor($width, $height);
	
	header("Content-type: image/jpeg");	
	$img = ImageCreateFromjpeg($map);

if (count($pathinfo)>1)
{
	$strlen = min(255, strlen($t));
	for($i=0;$i<$strlen;$i++)
	{	
		$color = $pathinfo[$i][4];
		$int = hexdec($color);
		$arr = array("red" => 0xFF & ($int >> 0x10),
		"green" => 0xFF & ($int >> 0x8),
		"blue" => 0xFF & $int);
	
		$clr = ImageColorAllocate($img, $arr["red"], $arr["green"], $arr["blue"]); 

		imagettftext($img, $pathinfo[$i][0], $pathinfo[$i][1], $pathinfo[$i][2], $pathinfo[$i][3], $clr, $basedir."/".$pathinfo[$i][5], $t[$i]);
		
	}
}
else
{
	//single line
	$color = $pathinfo[0][4];
	$int = hexdec($color);
	$arr = array("red" => 0xFF & ($int >> 0x10),
		"green" => 0xFF & ($int >> 0x8),
		"blue" => 0xFF & $int);
	
	$clr = ImageColorAllocate($img, $arr["red"], $arr["green"], $arr["blue"]); 
	$xandwidth = explode(",", $pathinfo[0][2]);


	imagettftextbox($img, $pathinfo[0][0], $pathinfo[0][1], $xandwidth[0], $pathinfo[0][3], $clr, $basedir."/".$pathinfo[0][5], $t, $xandwidth[1]);
}

	Imagecopyresampled($image_p, $img, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

	
	Imagejpeg($image_p); 

	ImageDestroy($img);
	ImageDestroy($image_p);	




function imagettftextbox(&$image, $size, $angle, $left, $top, $color, $font, $text, $max_width)
{
        $text_lines = explode("\n", $text); // Supports manual line breaks!
       
        $lines = array();
        $line_widths = array();
       
        $largest_line_height = 0;
       
        foreach($text_lines as $block)
        {
            $current_line = ''; // Reset current line
           
            $words = explode(' ', $block); // Split the text into an array of single words
           
            $first_word = TRUE;
           
            $last_width = 0;
           
            for($i = 0; $i < count($words); $i++)
            {
                $item = $words[$i];
                $dimensions = imagettfbbox($size, $angle, $font, $current_line . ($first_word ? '' : ' ') . $item);
                $line_width = $dimensions[2] - $dimensions[0];
                $line_height = $dimensions[1] - $dimensions[7];
               
                if($line_height > $largest_line_height) $largest_line_height = $line_height;
               
                if($line_width > $max_width && !$first_word)
                {
                    $lines[] = $current_line;
                   
                    $line_widths[] = $last_width ? $last_width : $line_width;
                                                         
                    $current_line = $item;
                }
                else
                {
                    $current_line .= ($first_word ? '' : ' ') . $item;
                }
               
                if($i == count($words) - 1)
                {
                    $lines[] = $current_line;
                   
                    $line_widths[] = $line_width;
                }
               
                $last_width = $line_width;
                   
                $first_word = FALSE;
            }
           
            if($current_line)
            {
                $current_line = $item;
            }
        }
       
        $i = 0;
        foreach($lines as $line)
        {
            
            imagettftext($image, $size, $angle, $left, $top + $largest_line_height + ($largest_line_height * $i), $color, $font, $line);
            $i++;
        }
       
        return $largest_line_height * count($lines);
}

function getRegistry($basedir)
{
	$registry = array();
	$handler = opendir($basedir);

	while ($file = readdir($handler))
	{
		if ($file != '.' && $file != '..')
		{
			if (substr($file, -3) == "php")
			{
				$fileparts = explode(".", $file);
				$imagefile = $fileparts[0].".jpg";
				$regitem = array();
				array_push($regitem, "./img/".$imagefile);
				array_push($regitem, $basedir."/".$file);
				array_push($registry, $regitem);
			}
		}
	}
	closedir($handler);
	
	return $registry;
}
?> 
 
